package Eyjafjalla.patch;

import Eyjafjalla.modcore.EyjafjallaMod;
import basemod.ReflectionHacks;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Bezier;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.math.Vector2;
import com.evacipated.cardcrawl.modthespire.lib.SpireInsertPatch;
import com.evacipated.cardcrawl.modthespire.lib.SpirePatch;
import com.evacipated.cardcrawl.modthespire.lib.SpirePostfixPatch;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.cards.CardQueueItem;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.helpers.ImageMaster;
import com.megacrit.cardcrawl.helpers.MathHelper;
import com.megacrit.cardcrawl.helpers.input.InputHelper;
import com.megacrit.cardcrawl.monsters.AbstractMonster;

import java.util.Iterator;

public class HeartPatch {
    //主要是用来实现心音相关的Patch

    //首先是悬浮演示，当悬浮一张手中/云层的心音牌时，自动悬浮另一个区域的云层/手中的心音牌，这部分在CloudPatch中写

    //仅UI部分，实际打出部分在CloudPatch中

    private static float arrowX;
    private static float arrowY;
    private static Vector2 controlPoint = new Vector2();
    private static float arrowScale;
    private static float arrowScaleTimer;
    private static Vector2 arrowTmp = new Vector2();
    private static Vector2 startArrowVector = new Vector2();
    private static Vector2 endArrowVector = new Vector2();
    private static Vector2[] points;
    private static final Color ARROW_COLOR = new Color(1.0F, 0.2F, 0.3F, 1.0F);

    public static boolean AlwaysHeart = false;

    @SpirePatch(clz = AbstractPlayer.class,method = "releaseCard")
    public static class ReleaseCardPatch{
        @SpirePostfixPatch
        public static void Postfix(AbstractPlayer _inst){
            CloudPatch.heartCard = null;
        }
    }

    @SpirePatch(clz = AbstractPlayer.class,method = "playCard")
    public static class PlayCardPatch{
        @SpireInsertPatch(rloc = 5)
        public static void Insert(AbstractPlayer _inst){
            EyjafjallaMod.logSomething("====心音牌："+(CloudPatch.heartCard!=null?CloudPatch.heartCard.name:"无")+"====");
            if(CloudPatch.heartCard!=null){

                CloudPatch.CloudField.heartCard.set(_inst.hoveredCard,CloudPatch.heartCard);
            }
            CloudPatch.heartCard = null;
        }
    }

    private static boolean queueContains(AbstractCard card) {
        Iterator var2 = AbstractDungeon.actionManager.cardQueue.iterator();

        CardQueueItem i;
        do {
            if (!var2.hasNext()) {
                return false;
            }

            i = (CardQueueItem)var2.next();
        } while(i.card != card);

        return true;
    }

    @SpirePatch(clz = AbstractPlayer.class,method = "renderTargetingUi")
    public static class RenderTargetPatch{
        @SpirePostfixPatch
        public static void Postfix(AbstractPlayer _inst,SpriteBatch sb){
            renderTargetingUi(sb);
        }
    }


    private static void drawCurvedLine(SpriteBatch sb, Vector2 start,Vector2 end,Vector2 control){
        float radius = 7.0F * Settings.scale;

        for(int i = 0; i < points.length - 1; ++i) {
            points[i] = (Vector2) Bezier.quadratic(points[i], (float)i / 20.0F, start, control, end, arrowTmp);
            radius += 0.4F * Settings.scale;
            if (i != 0) {
                arrowTmp.x = points[i - 1].x - points[i].x;
                arrowTmp.y = points[i - 1].y - points[i].y;
                sb.draw(ImageMaster.TARGET_UI_CIRCLE, points[i].x - 64.0F, points[i].y - 64.0F, 64.0F, 64.0F, 128.0F, 128.0F, radius / 18.0F, radius / 18.0F, arrowTmp.nor().angle() + 90.0F, 0, 0, 128, 128, false, false);
            } else {
                arrowTmp.x = controlPoint.x - points[i].x;
                arrowTmp.y = controlPoint.y - points[i].y;
                sb.draw(ImageMaster.TARGET_UI_CIRCLE, points[i].x - 64.0F, points[i].y - 64.0F, 64.0F, 64.0F, 128.0F, 128.0F, radius / 18.0F, radius / 18.0F, arrowTmp.nor().angle() + 270.0F, 0, 0, 128, 128, false, false);
            }
        }
    }

    private static void renderTargetingUi(SpriteBatch sb){
        AbstractCard c = CloudPatch.heartCard;
        AbstractMonster m = (AbstractMonster) ReflectionHacks.getPrivate(AbstractDungeon.player,AbstractPlayer.class,"hoveredMonster");
        if(c==null)
            return;
        arrowX = MathHelper.mouseLerpSnap(arrowX,(float) InputHelper.mX);
        arrowY = MathHelper.mouseLerpSnap(arrowY,(float) InputHelper.mY);
        controlPoint.x = c.current_x - (arrowX-c.current_x)/4F;
        controlPoint.y = arrowY + (arrowY-c.current_y)/2F;
        if(m==null){
            arrowScale = Settings.scale;
            arrowScaleTimer = 0F;
            sb.setColor(Color.WHITE);
        }
        else {
            arrowScaleTimer += Gdx.graphics.getDeltaTime();
            if(arrowScaleTimer>1F)
                arrowScaleTimer=0F;
            arrowScale = Interpolation.elasticOut.apply(Settings.scale,Settings.scale*1.2F,arrowScaleTimer);
            sb.setColor(ARROW_COLOR);
        }
        arrowTmp.x = controlPoint.x - arrowX;
        arrowTmp.y = controlPoint.y - arrowY;
        arrowTmp.nor();
        startArrowVector.x = c.current_x;
        startArrowVector.y = c.current_y;
        endArrowVector.x = arrowX;
        endArrowVector.y = arrowY;
        drawCurvedLine(sb,startArrowVector,endArrowVector,controlPoint);
        sb.draw(ImageMaster.TARGET_UI_ARROW, arrowX - 128.0F, arrowY - 128.0F, 128.0F, 128.0F, 256.0F, 256.0F, arrowScale, arrowScale, arrowTmp.angle() + 90.0F, 0, 0, 256, 256, false, false);
    }

    static {
        points = new Vector2[20];
        for(int i = 0; i < points.length; ++i) {
            points[i] = new Vector2();
        }
    }

}
